package com.ydgk.servlet;

import com.ydgk.bean.Product;
import com.ydgk.bean.User;
import com.ydgk.service.ProductService;
import com.ydgk.service.UserService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "UserServlet", urlPatterns = "/UserServlet.action")
public class UserServlet extends BaseServlet {
    protected void findUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String phone=request.getParameter("phone");
        String pn=request.getParameter("pageNumber");
        UserService userService= new UserService();
        Integer pageNumber=null;
        try {
            pageNumber = Integer.parseInt(pn);
        }catch (NumberFormatException e){
            pageNumber = 1 ;
        }
        Map map=userService.AdminSearch(phone,pageNumber);
        List<User> userList=(List<User>)map.get("userList");
        for (User user : userList) {
            System.out.println("user = " + user);
        }
        String pageCode = (String)map.get("pageCode");
        request.setAttribute("phone",phone);
        request.setAttribute("userList",userList);
        request.setAttribute("pageCode",pageCode);
        request.setAttribute("s","1");
        request.getRequestDispatcher("UserManagement.jsp").forward(request,response);
    }
    protected void saveOrUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = null ;
        try{
             id=Integer.parseInt(request.getParameter("id"));
        }catch (NumberFormatException e){
        }
        String name=request.getParameter("name");
        String address=request.getParameter("address");
        String phone=request.getParameter("phone");
        User user=new User();
        user.setId(id);
        user.setPhone(phone);
        user.setName(name);
        user.setAddress(address);
        UserService userService=new UserService();
        int i = userService.saveOrUpdate(user);
        if(i>0){
            response.sendRedirect("UserServlet.action?flag=findUser");
        }else{
            response.getWriter().print("操作失败");
        }
    }
    protected void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user=new User();
        UserService userService=new UserService();
        String id=request.getParameter("id");
        Integer i=null;
        try {
           i= userService.deleteById(Integer.parseInt(id));
        }catch (NumberFormatException e){
        }
        PrintWriter out=response.getWriter();
        out.print(i>0?true:false);
    }
    protected void deletes(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user=new User();
        UserService userService=new UserService();
        String ids=request.getParameter("ids");
        String[] s=ids.split(",");
        Integer i=null;
        for (String s1 : s) {
            try{
                i=userService.deleteById(Integer.parseInt(s1));
            }catch (NumberFormatException e) {
            }
            System.out.println(i);
        }
        PrintWriter out=response.getWriter();
        out.print(i>0?true:false);
    }
}
